package com.javaclimb.music.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.javaclimb.music.domain.ListSong;
import com.javaclimb.music.service.ListSongService;
import com.javaclimb.music.utils.PageUtils;
import com.javaclimb.music.utils.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 歌单的歌曲管理controller
 */
@RestController
@RequestMapping("/listSong")
@Api(tags = "歌单中的歌曲")
public class ListSongController {

    @Autowired
    private ListSongService listSongService;

    /**
     * 给歌单添加歌曲
     */
    @PostMapping(value = "/add")
    public R addListSong(@RequestBody ListSong listSong) {
        //TODO 添加歌单中已经存在的歌曲处理
        boolean flag = listSongService.save(listSong);
        if (flag) {
            return R.ok("保存成功").put("data", listSong);
        }
        return R.error("保存失败");
    }

    /**
     * 根据歌单id查询歌曲 需要传递参数songListId
     */
    @GetMapping("/detail")
    public R detail(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = listSongService.queryPage(params);
        return R.ok().put("data", pageUtils);
    }

    /**
     * 删除歌单里的歌曲
     */
    @PostMapping("/delete")
    public R delete(@RequestBody ListSong listSong) {
        boolean flag = listSongService.remove(new LambdaQueryWrapper<ListSong>()
                .eq(ListSong::getSongId, listSong.getSongId())
                .eq(ListSong::getSongListId, listSong.getSongListId()));
        if (flag) {
            return R.ok();
        }
        return R.error("删除失败");
    }

    /**
     * 根据歌单ids批量删除歌单里的歌曲
     */
    @PostMapping("/deleteByIds")
    public R delete(@RequestBody Integer[] ids) {
        boolean flag = listSongService.removeByIds(Arrays.asList(ids));
        if (flag) {
            return R.ok();
        }
        return R.error("删除失败");
    }

}




















